<template>
	<view class="container">
		<uni-section title="活动发布" type="line"></uni-section>
		<view class="example">
			<!-- 基础用法，不包含校验规则 -->
			<uni-forms ref="baseForm" :modelValue="baseFormData">
				<uni-forms-item label="标题" required><uni-easyinput v-model="baseFormData.title" placeholder="请输入标题" /></uni-forms-item>
				<uni-forms-item label="内容" required><uni-easyinput type="textarea" v-model="baseFormData.content" placeholder="请输入内容" /></uni-forms-item>
				<uni-forms-item label="开始时间" required><uni-datetime-picker type="date" return-type="string" v-model="baseFormData.datetimesingleBegin" /></uni-forms-item>
				<uni-forms-item label="结束时间" required><uni-datetime-picker type="date" return-type="string" v-model="baseFormData.datetimesingleEnd" /></uni-forms-item>
				<uni-forms-item label="截止时间" required><uni-datetime-picker type="date" return-type="string" v-model="baseFormData.datetimesingleEndEnroll" /></uni-forms-item>
				<uni-forms-item label="电话" required><uni-easyinput v-model="baseFormData.phone" placeholder="请输入活动电话" /></uni-forms-item>
				<uni-forms-item label="时长(小时)" required><uni-easyinput v-model="baseFormData.durationHour" placeholder="请输入志愿时长(小时)" /></uni-forms-item>
				<uni-forms-item label="时长(分钟)" required><uni-easyinput v-model="baseFormData.durationMin" placeholder="请输入志愿时长(分钟)" /></uni-forms-item>
				<uni-forms-item label="报名人数" required><uni-easyinput v-model="baseFormData.number" placeholder="请输入活动最大报名人数" /></uni-forms-item>
				<uni-forms-item label="报名要求" required><uni-easyinput v-model="baseFormData.order" placeholder="请输入活动报名要求" /></uni-forms-item>
				<picker class="address" mode="region" @change="bindAddressChange">点击选择活动地址</picker>
				<view class="addressText">{{ address }}</view>
				<view class="line4">————————————————————————————————————————————————————</view>
				<uni-forms-item label="详细地址" required><uni-easyinput v-model="baseFormData.place" placeholder="请输入详细活动地址" /></uni-forms-item>
			</uni-forms>
			<button class="btn" @click="insertActivity">发布</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			address: '',
			isAfter: 1,
			isBefore: 1,
			isRight: 1,
			// 基础表单数据
			baseFormData: {
				title: '',
				content: '',
				datetimesingleBegin: '',
				datetimesingleEnd: '',
				datetimesingleEndEnroll: '',
				place: '',
				phone: '',
				durationHour: '',
				durationMin: '',
				number: '',
				order: ''
			}
		};
	},

	onLoad() {},
	onReady() {
		// 设置自定义表单校验规则，必须在节点渲染完毕后执行
		// this.$refs.customForm.setRules(this.customRules);
	},
	methods: {
		validateForm() {
			let isValid = true;
			// 获取当前时间
			let now = new Date();
			// 获取两天后的时间
			let twoDaysLater = new Date(now.getTime() + 2 * 24 * 60 * 60 * 1000);
			// 获取开始时间
			let startTime = new Date(this.baseFormData.datetimesingleBegin);
			if (startTime <= twoDaysLater) {
			    // 开始时间早于现在时间的两天后
			    console.log('发布活动需要提前两天！');
				isValid = true;
				this.isRight = 0;
			}else{
				this.isRight = 1;
			}
			// Check if the required fields are not empty
			if (!this.baseFormData.title) {
				isValid = false;
				console.log('请输入标题');
			}
			if (new Date(this.baseFormData.datetimesingleBegin) > new Date(this.baseFormData.datetimesingleEnd)) {
				isValid = true;
				this.isAfter = 0;
				// your code here
			}else{
				this.isAfter = 1;
			}
			if (new Date(this.baseFormData.datetimesingleBegin) < new Date(this.baseFormData.datetimesingleEndEnroll)) {
				isValid = true;
				this.isBefore = 0;
				// your code here
			}else{
				this.isBefore = 1;
			}
			if (!this.baseFormData.content) {
				isValid = false;
				console.log('请输入内容');
			}
			if (!this.baseFormData.datetimesingleBegin) {
				isValid = false;
			}
			if (!this.baseFormData.datetimesingleEnd) {
				isValid = false;
			}
			if (!this.baseFormData.datetimesingleEndEnroll) {
				isValid = false;
			}
			if (!this.baseFormData.phone) {
				isValid = false;
				console.log('请输入活动电话');
			}

			if (!this.baseFormData.durationHour) {
				isValid = false;
				console.log('请输入志愿时长(小时)');
			}
			
			if (!this.baseFormData.durationMin) {
				isValid = false;
				console.log('请输入志愿时长(分钟)');
			}

			if (!this.baseFormData.number) {
				isValid = false;
				console.log('请输入活动最大报名人数');
			}

			if (!this.baseFormData.order) {
				isValid = false;
				console.log('请输入活动报名要求');
			}
			if (!this.address) {
				isValid = false;
			}

			if (!this.baseFormData.place) {
				isValid = false;
				console.log('请输入详细活动地址');
			}

			return isValid;
		},
		bindAddressChange: function(e) {
			this.region = e.target.value;
			this.address = this.region;
			console.log(this.region);
		},
		insertActivity() {
			let that = this;
			console.log()
			if (!this.validateForm()) {
				console.log('请完成上面内容的全部填写');
				uni.showToast({
					icon: 'none',
					title: '请完成所有内容的填写'
				});
				return;
			}else if(that.isAfter == 0){
				console.log('开始时间不能晚于结束时间');
				uni.showToast({
					icon: 'none',
					title: '开始时间不能晚于结束时间'
				});
				return;
			}else if(that.isBefore == 0){
				console.log('报名截止时间不能早不能晚于开始时间');
				uni.showToast({
					icon: 'none',
					title: '报名截止时间不能早不能晚于开始时间'
				});
				return;
			}else if(that.isRight == 0){
				console.log('发布活动需要提前两天');
				uni.showToast({
					icon: 'none',
					title: '发布活动需要提前两天'
				});
				return;
			}
			let data = {
				title: that.baseFormData.title,
				content: that.baseFormData.content,
				province: that.address[0],
				city: that.address[1],
				district: that.address[2],
				detailedPlace: that.baseFormData.place,
				tel: that.baseFormData.phone,
				beginTime: that.baseFormData.datetimesingleBegin,
				endTime: that.baseFormData.datetimesingleEnd,
				endEnrollTime: that.baseFormData.datetimesingleEndEnroll.toString(),
				durationHour: that.baseFormData.durationHour,
				durationMin: that.baseFormData.durationMin,
				ord: that.baseFormData.order,
				applicantNum: that.baseFormData.number
			};
			console.log(that.address[2]);
			that.ajax(that.url.insertActivity, 'POST', data, function(resp) {
				let result = resp.data.result;
				console.log(result);

				if (result === '活动发布成功！') {
					uni.showToast({
						title: '活动发布成功！',
						icon: 'none'
					});
					setTimeout(function() {
						(that.baseFormData.content = ''),
							(that.baseFormData.datetimesingleBegin = ''),
							(that.baseFormData.datetimesingleEnd = ''),
							(that.baseFormData.datetimesingleEndEnroll = ''),
							(that.baseFormData.duration = ''),
							(that.baseFormData.number = ''),
							(that.baseFormData.order = ''),
							(that.baseFormData.phone = ''),
							(that.baseFormData.place = ''),
							(that.baseFormData.title = ''),
							(that.address = ''),
							uni.switchTab({
								url: '/pages/index/index'
							});
					}, 1000);
				}
			});
		},
		onClickItem(e) {
			console.log(e);
			this.current = e.currentIndex;
		},
		add() {
			this.dynamicLists.push({
				label: '域名',
				rules: [
					{
						required: true,
						errorMessage: '域名项必填'
					}
				],
				id: Date.now()
			});
		},
		del(id) {
			let index = this.dynamicLists.findIndex(v => v.id === id);
			this.dynamicLists.splice(index, 1);
		}
	}
};
</script>

<style lang="less">
@import url('publish.less');
</style>
